package com.atguigu.cloud.controller;

import com.atguigu.cloud.service.FlowLimitService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {

    @GetMapping(value = "/sentinel/A")
    public String testA() {
        return "------Test A";
    }

    @GetMapping(value = "/sentinel/B")
    public String testB() {
        return "------Test B";
    }

    /**
     * 流控: 若C和D都请求访问flowLimitService.common()方法,阈值到达后对C限流,对D不管, C:在sentinel配置每秒1QPS
     */
    @Resource
    private FlowLimitService flowLimitService;

    @GetMapping(value = "/sentinel/C")
    public String testC() {
        flowLimitService.common();
        return "------Test C";
    }

    @GetMapping(value = "/sentinel/D")
    public String testD() {
        flowLimitService.common();
        return "------Test D";
    }

    /**
     * 流控效果 —— 排队等待
     *  sentinel配置每秒1QPS, 10秒后的请求作为超时处理,丢弃
     * @return
     */
    @GetMapping(value = "/sentinel/E")
    public String testE() {
        System.out.println(System.currentTimeMillis() + "    testE, 流控效果----排队等待");
        return "------Test E";
    }

    /**
     * 熔断规则 —— 慢调用比例
     * @return
     */
    @GetMapping(value = "/sentinel/F")
    public String testF() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("--测试: 熔断规则 —— 慢调用比例");
        return "------Test F";
    }

    /**
     * 熔断规则 —— 异常比例
     * @return
     */
    @GetMapping(value = "/sentinel/G")
    public String testG() {
        System.out.println("--测试: 熔断规则 —— 异常比例");
        int age = 10 / 0;
        return "------Test G";
    }

    /**
     * 熔断规则 —— 异常数
     * @return
     */
    @GetMapping(value = "/sentinel/H")
    public String testH() {
        System.out.println("--测试: 熔断规则 —— 异常数");
        int age = 10 / 0;
        return "------Test H";
    }

}
